#include "CWinForm.h"


#pragma region Constructor
CWinForm::CWinForm(void)
{
}


CWinForm::~CWinForm(void)
{
}

CWinForm::CWinForm(HINSTANCE hInst)
{
	MyRegisterClass(hInst);
	if(!Create(hInst))
	{
		MessageBox(NULL,"Create Window - Failed","Error",MB_OK | MB_ICONERROR);
		return;
	}
}
#pragma  endregion

#pragma region Methods

/// Register Window Class
bool CWinForm::MyRegisterClass(HINSTANCE hInst)
{
	WNDCLASSEX wc;
	wc.cbClsExtra = 0;
	wc.cbSize = sizeof(WNDCLASSEX);
	wc.cbWndExtra = 0;
	wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
	wc.hCursor = LoadCursor(NULL,IDC_ARROW);
	wc.hIcon = NULL;
	wc.hIconSm = NULL;
	wc.hInstance = hInst;
	wc.lpfnWndProc = (WNDPROC)WinProc;
	wc.lpszClassName = "Prinny";
	wc.lpszMenuName = NULL;
	wc.style = CS_HREDRAW | CS_VREDRAW;

	return RegisterClassEx(&wc);
}

/// Get HWND
HWND CWinForm::GetHWND()
{
	return m_hWnd;
}

bool CWinForm::Create(HINSTANCE hInst)
{
	m_hWnd = CreateWindow(
		"Prinny",
		"Prinny",
		WS_OVERLAPPEDWINDOW,
		CW_USEDEFAULT,
		CW_USEDEFAULT,
		800,
		640,
		NULL,
		NULL,
		hInst,
		NULL);

	if(!m_hWnd)
	{
		return false;
	}

	return true;
}

/// Show and Update Window
void CWinForm::Show()
{
	ShowWindow(m_hWnd,SW_SHOW);
	UpdateWindow(m_hWnd);
}

/// 
LRESULT CALLBACK CWinForm::WinProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	switch(msg)
	{
	case WM_KEYDOWN:

		switch(wParam)
		{
		case VK_ESCAPE:

			int e = MessageBox(NULL,"Quit program ?","",MB_YESNO | MB_ICONQUESTION);
			if(e==IDYES)
			{
				DestroyWindow(hWnd);

			}

			break;

		}
		return 0;

	case WM_QUIT:
		DestroyWindow(hWnd);
		return 0;

	case WM_DESTROY:
		PostQuitMessage(0);
		return 0;

	}
	return DefWindowProc(hWnd,msg,wParam,lParam);
}
#pragma endregion 
